<?php

/*
  Project: Blogmer - Open Source Blogging Software
  Type: PHP
  Desc: Contains all functions related to categories.
  Author: Yashpal
  Usage:  	$this->load->model('Categories_model','',TRUE);
			$cats=$this->Categories_model->Grab_Cats();  - mandatory
			
			$catstring1=$this->Categories_model->SelectCategories($cats); - optional
			$catstring2=$this->Categories_model->CreateLiCategories($cats); - optional
			$catstring3=$this->Categories_model->CreateCheckboxCategories($cats); - optional
			
			

  Function name : 
  Grab_Cats          -> This function will find all details of categories.
  				        Give an 2D array containing all values of categories.
  SelectCategories   -> This function will give Select List HTML Code of categories.
  						HTMl Select with categories value
  CreateLicategories -> This function will give LI List HTML Code of categories.
						HTMl li version with categories valu
 

*/


class Categories_model extends Model {
	

    function Categories_model()

    {
        // Call the Model constructor
        parent::Model();
    }

		//Function Name - Grab_Cats
		//This function will find all details of categories.
		//Parram - No input
		//output - Give an 2D array containing all values of categories
		function Grab_Cats()
		{		 				
			$query = $this->db->get('categories');
			$categories = $query->result_array();
			return $categories; 	
		}

   //$page_id=self::RefinePagNav($page_id);
    function RefinePagNav($page_id)
  	{
  			if($page_id==1)
  			$page_id=0;
  			
  			return $page_id;
  	}			
		function save_category()
		{
		/*	CatID Cat RSS CatPrivate ParentID*/

			$post= array(
						'CatID'=>"",
						'Cat' =>  $this->input->post('category'),
						'RSS'=>"",
						'CatPrivate' => "",
						'ParentID' => $this->input->post('listcategory')
						);	
				
		
			$result=$this->db->insert('categories', $post);
			
			if($result)
				$data['info']=$this->input->post('category')." category created successfully";
			else
				$data['info']="Create category failed";
			
			$data['title']="Information Page";
			$data['header']="Information";
			$data['url']="/admin/CreateCategory";
			$data['time']="2";
			
					
			$this->load->view('admin/info',$data);
			
			//redirect('/admin/CreateCategory','refresh');
		}
		
		function edit_category($cat_id,$page_id)
		{
		/*	CatID Cat RSS CatPrivate ParentID*/

			$post= array(
						'Cat' =>  $this->input->post('category'),
						'ParentID' => $this->input->post('listcategory')
						);	
			$page_id=self::RefinePagNav($page_id);	
		
			$this->db->where(array('CatID'=>$cat_id));
			$result=$this->db->update('categories', $post);
			//redirect('/admin/ListCategories/'.$pageid,'refresh');
			
			if($result)
				$data['info']="category updated successfully";
			else
				$data['info']="category updation failed";
			
			$data['title']="Information Page";
			$data['header']="Information";
			$data['url']='/admin/ManageCategories/'.$page_id;
			$data['time']="1";
			
				
			$this->load->view('admin/info',$data);
		}
		
		function delete_category($catid,$page_id)
		{
		  $page_id=self::RefinePagNav($page_id);
		  $this->db->where('CatID', $catid);
		  if($catid==1)
		  {
			$data['time']="3";
			$data['info']="Default category can't be deleted";
		  }
		  else
		  {
	  		$result=$query=$this->db->delete('categories');
			//redirect('/admin/ManageCategories/'.$pageid, 'refresh');
			if($result)
				$data['info']="category deleted successfully";
			else
				$data['info']="category deletion failed";
		  }		
			
			$data['title']="Information Page";
			$data['header']="Information";
			$data['url']='/admin/ManageCategories/'.$page_id;
			$data['time']="1";
			
					
			$this->load->view('admin/info',$data);
		}
		
		
		//Function Name - SelectCategories
		//This function will give Select List HTML Code of categories.
		//Input - 2D categories array
		//Output - HTMl Select with categories value
		function SelectCategories($categories) 
		{
		// $catstring will hold the output
						$query = $this->db->get('categories');

			
		$catstring= "<select name=\"SelectCategories\" id=\"SelectCategories\">";
			for($i=0;$i<count($categories);$i++ )
			{
				$catstring.="<option value=\"".$categories[$i]['CatID']."\">".$categories[$i]['Cat']."</option>";
			}
			$catstring.="</select>";
			
		return 	$catstring;
			
		}
		
		function categories_list() 
		{
		// $catstring will hold the output
			
			$query = $this->db->get('categories');
			$strr="<select name=\"categorylist\">";
			$strr.="<option value=\"0\">None</option>";
			foreach ($query->result() as $row)
			{

				$strr.="<option value=\"".$row->CatID."\">".$row->Cat."</option>";				

			}
			$strr.="</select>";
			return $strr;
		
		
			
		}
		
		function find_cat($catid) 
		{
		// $catstring will hold the output
			$this->db->where(array('CatID'=>$catid));
			$query = $this->db->get('categories');
			
			foreach ($query->result() as $row)
			{
				$strr=$row->Cat	;
			}

			return $strr;
					
		}
		
		
		function list_categories($pageid=1)
		{
			$this->db->from('categories');
			$cnt=$this->db->count_all_results();
			
			$config['base_url'] = site_url().'/admin/ManageCategories/';
			$config['total_rows'] =$cnt;
			$config['per_page'] = '10';
			$config['num_links'] = '3';
			$config['uri_segment'] = '3';
	
	    $pageid=self::RefinePagNav($pageid);
			$this->pagination->initialize($config); 
//			$this->db->order_by("CatID", "desc"); 
			$query = $this->db->get('categories',$config['per_page'],$this->uri->segment(3));
			
			$out= "<table width=\"100%\" border=\"0\">";
			$out.="<caption>Categories List</caption>
			<thead>
				<tr>
					<th scope=\"col\">Category ID</th>
					<th scope=\"col\">Category</th>
					<th scope=\"col\">Edit</th>
					<th scope=\"col\">Delete</th>
				</tr>
			</thead>	
			<tfoot>
				<tr>
					<th scope=\"row\">Total</th>

					<td colspan=\"3\">$cnt Categories</td>
				</tr>
			</tfoot>
			<tbody>";	
	
			foreach ($query->result() as $row)
			{
				$out.=
				 "<tr>
					<td>".$row->CatID."</td>
					<td>".$row->Cat."</td>
					<td>".anchor('admin/EditCategory/'.$row->CatID.'/'.$pageid,'Edit')."</td>
					<td>".anchor('admin/DeleteCategory/'.$row->CatID.'/'.$pageid,'Delete')."</td>
				  </tr>";
				
				
			} 
			$out.= "</tbody></table>";
			$out.= $this->pagination->create_links();
			return $out;
			
		}
		
		
		
		
		function CheckboxCategories($checkid=array('1'))
		{
			
			//$check
			//$checkid=implode(',', $check);
			$query = $this->db->get('categories');
			$strr='<ul style="list-style:none">';
			$j=0;
			foreach ($query->result() as $row)
			{
				$strr.='<li>
						 <label>
						 	<input name="ccat[]" type="checkbox" ';
							
							
							for($i=0;$i<count($checkid);$i++)
								if($row->CatID==$checkid[$i])
									$strr.='checked="checked"';
					
				
				$strr.=' value="'.$row->CatID.'" />&nbsp;&nbsp;&nbsp;'.$row->Cat.'</label></li>';
		
			}
			$strr.="</ul>";
			return $strr;
			
			
		}
		
		//Function Name - CreateCheckboxcategories
		//This function will give LI List HTML Code of categories.
		//Input - 2D categories array
		//Output - HTMl li version with categories value
		function CreateCheckboxCategories($categories)
		{
			//give starting values
			$subcreateli[2]="";
			for($i=0;$i<count($categories);$i++ )
			{
				//check for all parent categories and add to createli array
				if($categories[$i]['ParentID']==0)
				{
				$createli[$categories[$i]['CatID']] ="<li><label><input name=\"ccat[]\" type=\"checkbox\" ".$this->validation->set_checkbox('ccat[]', '1')." value=".$categories[$i]['CatID']." />".$categories[$i]['Cat']."</label></li>";
				//"<li>".$categories[$i]['Cat']."</li>";
				$subcreateli[$categories[$i]['CatID']]=""; //testing only
				}
				else
				{
				//now check and subcategories and add them to a new subcreateli array.
				$subcreateli[$categories[$i]['ParentID']].="<li><label><input name=\"ccat[]\" type=\"checkbox\" ".$this->validation->set_checkbox('ccat[]', '1')." value=".$categories[$i]['ParentID']." />".$categories[$i]['Cat']."</label></li>";
				$createli[$categories[$i]['CatID']]=""; //testing only
				}
			}
			
			//now compare subcreateli index with createli index matches will be added for new result
			
			for($i=0;$i<count($categories)-1;$i++ ) 
			{
				if($categories[$i]['ParentID']!=0)
				{	 $createli[$i].="<ul>".$subcreateli[$i]."</ul>";			}
			}
			
			//finally we creating HTML LI version of categories 
			$maincreateli="<ul>";
			for($i=1;$i<=count($createli);$i++ )
			$maincreateli.=$createli[$i];
			$maincreateli.="</ul>";			
			return $maincreateli;
			
			
			
		}
		
		function CreateLiCategories($categories)
		{
			//give starting values
			$subcreateli[2]="";
			for($i=0;$i<count($categories);$i++ )
			{
				//check for all parent categories and add to createli array
				if($categories[$i]['ParentID']==0)
				{
					$createli[$categories[$i]['CatID']] ="<li><a href=\"category/".$categories[$i]['Cat']."\">".$categories[$i]['Cat']."</a></li>";
					$subcreateli[$categories[$i]['CatID']]=""; 
				}
				else
				{
					//now check and subcategories and add them to a new subcreateli array.
					$subcreateli[$categories[$i]['ParentID']].="<li><a href=\"category/".$categories[$i]['Cat']."\">".$categories[$i]['Cat']."</a></li>";
					$createli[$categories[$i]['CatID']]=""; 
				}
			}
			
			//now compare subcreateli index with createli index matches will be added for new result
			
			for($i=0;$i<count($categories)-1;$i++ ) 
			{
				if($categories[$i]['ParentID']!=0)
				{	 $createli[$i].="<ul>".$subcreateli[$i]."</ul>";			}
			}
			
			//finally we creating HTML LI version of categories 
			$maincreateli="<ul>";
			for($i=1;$i<=count($createli);$i++ )
			$maincreateli.=$createli[$i];
			$maincreateli.="</ul>";			
			return $maincreateli;
		}
		
		

}

